<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="快递公司id" prop="courierId">
                      <el-input v-model="formData.courierId" placeholder="请输入快递公司id" />
                    </el-form-item>
                    <el-form-item label="优惠券类型(满减、会员折扣)" prop="couponType">
                      <el-select v-model="formData.couponType" placeholder="请选择优惠券类型(满减、会员折扣)">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DELIVERY_COUPONTYPE)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="折扣金额(满减券)" prop="discountPrice">
                      <el-input v-model="formData.discountPrice" placeholder="请输入折扣金额(满减券)" />
                    </el-form-item>
                    <el-form-item label="折扣率(折扣券)" prop="discountRate">
                      <el-input v-model="formData.discountRate" placeholder="请输入折扣率(折扣券)" />
                    </el-form-item>
                    <el-form-item label="最低订单金额要求(满减券)" prop="minPrice">
                      <el-input v-model="formData.minPrice" placeholder="请输入最低订单金额要求(满减券)" />
                    </el-form-item>
                    <el-form-item label="开始日期" prop="startDate">
                      <el-date-picker clearable v-model="formData.startDate" type="date" value-format="timestamp" placeholder="选择开始日期" />
                    </el-form-item>
                    <el-form-item label="结束日期" prop="endDate">
                      <el-date-picker clearable v-model="formData.endDate" type="date" value-format="timestamp" placeholder="选择结束日期" />
                    </el-form-item>
                    <el-form-item label="是否使用(默认false)" prop="isUsed">
                      <el-input v-model="formData.isUsed" placeholder="请输入是否使用(默认false)" />
                    </el-form-item>
                    <el-form-item label="版本号" prop="version">
                      <el-input v-model="formData.version" placeholder="请输入版本号" />
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as DeliveryCouponApi from '../../../../api/ipark/delivery/deliverycoupon';
      export default {
    name: "DeliveryCouponForm",
    components: {
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            courierId: undefined,
                            couponType: undefined,
                            discountPrice: undefined,
                            discountRate: undefined,
                            minPrice: undefined,
                            startDate: undefined,
                            endDate: undefined,
                            isUsed: undefined,
                            version: undefined,
        },
        // 表单校验
        formRules: {
        },
                        };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await DeliveryCouponApi.getDeliveryCoupon(id);
            this.formData = res.data;
            this.title = "修改快递服务-优惠券";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增快递服务-优惠券";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await DeliveryCouponApi.updateDeliveryCoupon(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await DeliveryCouponApi.createDeliveryCoupon(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            courierId: undefined,
                            couponType: undefined,
                            discountPrice: undefined,
                            discountRate: undefined,
                            minPrice: undefined,
                            startDate: undefined,
                            endDate: undefined,
                            isUsed: undefined,
                            version: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>
